package com.cy.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cy.common.util.IdWorker;
import com.cy.entity.Dictionary;
import com.cy.entity.UserVocabulary;
import com.cy.mapper.BookDictionaryMapper;
import com.cy.mapper.DictionaryMapper;
import com.cy.mapper.UserVocabularyMapper;
import com.cy.service.UserVocabularyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * @Author : 残影
 * @Date: 2021-02-27 11:44
 */
@Service
public class UserVocabularyServiceImpl extends ServiceImpl<UserVocabularyMapper, UserVocabulary> implements UserVocabularyService {
    @Autowired
    private DictionaryMapper dictionaryMapper;
    @Autowired
    private UserVocabularyMapper userVocabularyMapper;
    @Autowired
    private IdWorker idWorker;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addStudy(String bookId, String tag, Long userId) {
        List<Dictionary> dictionaryList = dictionaryMapper.getVocabulary(bookId, tag);
        if (!ObjectUtils.isEmpty(dictionaryList)){
            dictionaryList.forEach(dictionary -> {
                userVocabularyMapper.insert(UserVocabulary.builder()
                        .id(idWorker.nextId())
                        .userId(userId)
                        .bookId(bookId)
                        .chapterName(tag)
                        .vocabulary(dictionary.getVocabulary())
                        .remember(0)
                        .review(0).build());
            });
        }
        return dictionaryList.size();
    }
}
